<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/4
 * Time: 9:42
 */

/**
 * User model
 *
 * @property integer $id
 * @property string $uname
 * @property string $email
 * @property string $phone
 * @property string $pwd
 * @property string $login_salt
 * @property string $password_reset_token
 * @property integer $gender
 * @property string $face
 * @property string $profile
 * @property integer $project_num
 * @property integer $support_num
 * @property integer $create_time
 * @property integer $create_ip
 * @property integer $login_time
 * @property integer $login_ip
 * @property integer $is_del
 */
namespace app\common\model;

use think\Model;
use think\Request;

class User extends Model
{
    const GENDER_SECRECY = 0, //性别保密
        GENDER_MAN = 1,        //性别男
        GENDER_FEMALE = 2;     //性别女
    const DELETED = 1,         //删除
        NOT_DELETED = 0;       //未删除
    //protected $table = 'user';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = null;
    //注册ip自动完成
    protected $auto = ['create_ip'];

    /**
     * 注册ip自动完成
     * @return mixed
     */
    protected function setCreateIpAttr()
    {
        return Request::instance()->ip(1);
    }



    /**
     * 生成登录盐值
     * return $this
     */
    public function generateLoginSalt(){
        $this->login_salt = rand(10000,99999);
        return $this;
    }
    /**
     * 设置密码
     */
    public function setPassword($pwd){
        $this->pwd = md5(md5($pwd).$this->login_salt);
        return $this;
    }

    public function addComment(Archives $archives,$content,$pid=0){
        //创建评论模型
        $comment = new Comment();
        $comment->uid = $this->uid;
        $comment->aid = $archives->id;
        $comment->content = $content;
        $comment->pid = $pid;
        $comment->save();
        //文档评论数+1
        $archives->comment_num++;
        $archives->save();
    }

}